package com.cxd.app.service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cxd.app.entity.RechargeBankCard;
import com.cxd.app.entity.RechargeBankCardImpl;
import com.cxd.app.entity.base.RechargeBase;
import com.cxd.app.mapper.read.RRechargeMapper;
import com.cxd.base.common.constants.FeeConfigConstants.FeePoint;
import com.cxd.base.common.constants.FeeConfigConstants.FeeType;
import com.cxd.base.common.constants.UserConstants;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.ReqUtils;
import com.cxd.base.util.date.DateUtil;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2015年12月11日 上午10:36:01
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
@Service("rechargeService")
public class RechargeService {
	
	@Resource
	public BeanTools bt;
	
	@Resource
	public RRechargeMapper rechargeMapper;
	
	@Resource
	public FeeConfigService feeConfigService;
	
	
	@Transactional
	public String createRechargeOrder(RechargeBase recharge) {
		// 往recharge中插入值。
		recharge.setId(generateId());
		recharge.setFee(calculateFee(recharge.getActualMoney()));
		// 用rechargeWay进行判断，判断是要跳转到银行卡还是支付平台
		// recharge.setRechargeWay("借记卡");

		//	if (recharge.getCoupon() != null) {
		//		// 优惠券
		//		if (StringUtils.isEmpty(recharge.getCoupon().getId())) {
		//			recharge.setCoupon(null);
		//		} else {
		//			recharge.setCoupon(ht.get(UserCoupon.class, recharge
		//					.getCoupon().getId()));
		//			recharge.getCoupon().setStatus("已使用");
		//		}
		//	}

		recharge.setIsRechargedByAdmin("0");
		recharge.setTime(new Date());
		recharge.setStatus(UserConstants.RechargeStatus.WAIT_PAY);
		bt.add(recharge);
		return ReqUtils.getReq().getContextPath()+ "/to_recharge/" + recharge.getId();
	}
	
	/**
	 * 生成充值id
	 * 
	 * @return
	 */
	private synchronized  String generateId() {
		String gid = DateUtil.dateToString(new Date(), "yyyyMMdd");
		Map<String,Object> map = rechargeMapper.loadRechargeId(gid + "%");
		Integer itemp = 0;
		if(map!=null){
			String temp = map.get("ID").toString();
			temp = temp.substring(temp.length() - 6);
			itemp = Integer.valueOf(temp);
		}
		itemp++;
		gid += String.format("%08d", itemp);
		return gid;
	}
	public double calculateFee(double amount) {
		return feeConfigService.getFee(FeePoint.RECHARGE, FeeType.FACTORAGE, null,
				null, amount);
	}
	
	public String getBankNameByNo(final String bankNo){
		List<RechargeBankCard> banks = getBankCardsList();
		for(RechargeBankCard bank : banks){
			if(StringUtils.equals(bank.getNo(), bankNo)){
				return bank.getBankName();
			}
		}
		return "Not found Bank";
	}
	private static Properties props = new Properties(); 
	static{
		try {
			props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("banks.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public List<RechargeBankCard> getBankCardsList() {
		List<RechargeBankCard> bcs = new ArrayList<RechargeBankCard>();
		Iterator<Entry<Object, Object>> it = props.entrySet().iterator();  
		while(it.hasNext()){
			Entry<Object, Object> entry = it.next();  
            String key = (String) entry.getKey();  
            String value = (String) entry.getValue();  
			bcs.add(new RechargeBankCardImpl(key, value));
		}
		return bcs;
	}
}
